package com.jsmtr.rms.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Service;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.entity.HRZZScore;
import com.jsmtr.rms.entity.HRZZUser;
import com.jsmtr.rms.entity.ProjectInfo;
import com.jsmtr.rms.service.IRecommendService;
import com.jsmtr.rms.utils.BeanTools;
import com.jsmtr.rms.utils.EnumTools;
import com.jsmtr.rms.utils.db.DbUtil;

@Service
public class RecommendService implements IRecommendService {

    @Resource
    private DaoService dao;

    public void setDao(DaoService dao) {
        this.dao = dao;
    }

    @Override
    public Map<String, Object> getUsers(int page, int perpage, HRZZUser user,Map map) throws Exception {
       // StringBuffer queryHQL = new StringBuffer("select p.id,p.name,p.gender,p.code,p.major,p.graduationtime,p.bx,p.guake,p.height,p.job,p.state,p.csremarks,p.rstate,p.rmarks,u.userid,u.id drid,p.fid,p.fqstate,p.fqfid,p.njname,p.recommenddate from HR_ZZUSER p ,  DUserResume u   where 1=1 and p.code=u.cardno(+) and p.pid=u.proid(+) ");
        StringBuffer queryHQL = new StringBuffer("select p.* from HR_ZZUSER p ,  DUserResume u   where 1=1 and p.code=u.cardno(+) and p.pid=u.proid(+) ");
        StringBuffer countHQL = new StringBuffer("select count(1) from HR_ZZUSER p ,  DUserResume u   where 1=1 and p.code=u.cardno(+) and p.pid=u.proid(+) ");
        StringBuffer condition = new StringBuffer();
        Map<String, Object> _map = new HashMap<String, Object>();
       // condition.append(" and p.rid=u.id");
        condition.append(BeanTools.hqlWhere(user, "p"));
        Object agefrom = map.get("agefrom");
        Object ageto = map.get("ageto");
        Object heightfrom = map.get("heightfrom");
        Object heightto = map.get("heightto");
        Object graduationyear = map.get("graduationyear");
        Calendar cal = Calendar.getInstance();
        if (StringTools.notEmpty(agefrom)) {
            cal.setTime(new Date());
            cal.add(Calendar.YEAR, 0 - Integer.parseInt(agefrom.toString()));
           // String _date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);
           // SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            condition.append(" and substr(p.code,7,4)<='"+cal.get(Calendar.YEAR)+"' " );
        }
        if (StringTools.notEmpty(ageto)) {
            cal.setTime(new Date());
            cal.add(Calendar.YEAR, 0 - Integer.parseInt(ageto.toString()));
           // String _date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            condition.append(" and substr(p.code,7,4)>='"+cal.get(Calendar.YEAR)+"' ");
        }

        if (StringTools.notEmpty(heightfrom)) {
            condition.append(" and p.height>='"+heightfrom+"' ");
        }
        if (StringTools.notEmpty(heightto)) {
            condition.append(" and p.height<='"+heightto.toString()+"' ");
        }
        if (StringTools.notEmpty(graduationyear)) {
            condition.append(" and p.graduationtime like '"+graduationyear+"%'");
        }

        queryHQL.append(condition);
        countHQL.append(condition);
        //return DbUtil.pageSqlQueryMap(page,perpage,queryHQL.toString(),_map,countHQL.toString(),dao);
        return DbUtil.pageSqlQuery(page,perpage,queryHQL.toString(),countHQL.toString(),dao);
       // return DbUtil.pageQuery(page, perpage, queryHQL.toString(), countHQL.toString(), dao);
    }

    @Override
    public Map<String, Object> getUserMap(int page, int perpage, HRZZUser user, Map map) throws Exception {
        StringBuffer queryHQL = new StringBuffer("select p.*,u.userid,u.id drid,ms.point,ms.result, u.tjarrange, u.bdarrange from HR_ZZUSER p ,DoMS_User ms,  DUserResume u   where 1=1 and p.code=u.cardno(+) and p.pid=u.proid(+) and p.id=ms.userid(+) ");
        StringBuffer countHQL = new StringBuffer("select count(1) from HR_ZZUSER p ,DoMS_User ms,  DUserResume u   where 1=1 and p.code=u.cardno(+) and p.pid=u.proid(+) and p.id=ms.userid(+) ");
        StringBuffer condition = new StringBuffer();
        // condition.append(" and p.rid=u.id");
        condition.append(BeanTools.hqlWhere(user, "p"));
        Object agefrom = map.get("agefrom");
        Object ageto = map.get("ageto");
        Object heightfrom = map.get("heightfrom");
        Object heightto = map.get("heightto");
        Object graduationyear = map.get("graduationyear");
        Object tjarrange = map.get("tjarrange");
        Calendar cal = Calendar.getInstance();
        if (StringTools.notEmpty(agefrom)) {
            cal.setTime(new Date());
            cal.add(Calendar.YEAR, 0 - Integer.parseInt(agefrom.toString()));
            // String _date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);
            // SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            condition.append(" and substr(p.code,7,4)<='"+cal.get(Calendar.YEAR)+"' " );
        }
        if (StringTools.notEmpty(ageto)) {
            cal.setTime(new Date());
            cal.add(Calendar.YEAR, 0 - Integer.parseInt(ageto.toString()));
            // String _date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);
            //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            condition.append(" and substr(p.code,7,4)>='"+cal.get(Calendar.YEAR)+"' ");
        }

        if (StringTools.notEmpty(heightfrom)) {
            condition.append(" and p.height>='"+heightfrom+"' ");
        }
        if (StringTools.notEmpty(heightto)) {
            condition.append(" and p.height<='"+heightto.toString()+"' ");
        }
        if (StringTools.notEmpty(graduationyear)) {
            condition.append(" and p.graduationtime like '"+graduationyear+"%'");
        }
        if (StringTools.trimNotEmpty(tjarrange)){
            condition.append(" and p.tjaddress  "+tjarrange+" ");
        }

        Object usercontent = map.get("usercontent");
        if (StringTools.notEmpty(usercontent)){
            if (usercontent.toString().equalsIgnoreCase("已记录")){
                condition.append(" and  dr.content is not null ");
            }else  if (usercontent.toString().equalsIgnoreCase("未记录")){
                condition.append(" and  dr.content is  null ");
            }

        }

        Object ischangejob = map.get("ischangejob");
        if (StringTools.notEmpty(ischangejob)){
            if (ischangejob.toString().equalsIgnoreCase("是")){
                condition.append(" and  p.njname is not null ");
            }else  if (ischangejob.toString().equalsIgnoreCase("否")){
                condition.append(" and  p.njname is  null ");
            }

        }

        //2019-11-6 新增成绩区间条件筛选
        Object scorearea =map.get("scorearea");
        if (StringTools.notEmpty(map.get("scorearea"))) {
            if (scorearea.toString().equalsIgnoreCase("L60")){
                condition.append(" and to_number(nvl(ms.point,0))  <60 ");
            }else if(scorearea.toString().equalsIgnoreCase("R60")){
                condition.append(" and to_number(nvl(ms.point,0))  >=60 ");
            }

        }
        queryHQL.append(condition);
        countHQL.append(condition);
        return DbUtil.pageSqlQueryMap(page,perpage,queryHQL.toString(),null,countHQL.toString(),dao);
    }

    @Override
    public HRZZUser getViewUser(String id) throws Exception {
        return (HRZZUser) dao.findUnique("from HRZZUser where id='" + id + "'");
    }
    @Override
    public HRZZUser getViewUser(String id,String pid) throws Exception {
        return (HRZZUser) dao.findUnique("from HRZZUser where id='" + id + "' and pid='"+pid+"'");
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<HRZZUser> getuserbyids(String[] ids) throws Exception {
        return dao.find("from HRZZUser where id in(:idlist)", ids);
    }

    @Override
    public HRZZUser getUser(String pid, String code, String deptcode) throws Exception {
        return (HRZZUser) dao.findUnique("from HRZZUser where pid='" + pid + "' and code='" + code + "' and deptcode='" + deptcode + "'");
    }

    @Override
    public List<HRZZUser> getUserList(String pid, String code, String deptcode) throws Exception {
        return (List<HRZZUser>) dao.find("from HRZZUser where pid='" + pid + "' and code='" + code + "' and deptcode='" + deptcode + "'");
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ProjectInfo> getProject() throws Exception {
        StringBuilder hql = new StringBuilder("from ProjectInfo ");
        hql.append("where state='" + EnumTools.ProjectState.已发布.ordinal() + "' ");
        hql.append("and recruittype='" + EnumTools.RecruitType.中专校园招聘.ordinal() + "' ");
        hql.append("and enddate>=:end");
        return dao.find(hql.toString(), "end", new Date());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ProjectInfo> getAllProject() throws Exception {
        StringBuilder hql = new StringBuilder("from ProjectInfo ");
        hql.append("where state!='" + EnumTools.ProjectState.编辑中.ordinal() + "' ");
        hql.append("and recruittype='" + EnumTools.RecruitType.中专校园招聘.ordinal() + "' ");
        return dao.find(hql.toString());
    }

    @Override
    public ProjectInfo getProject(String pid) throws Exception {
        return (ProjectInfo) dao.findUnique("from ProjectInfo where id='" + pid + "'");
    }

    @Override
    public HRZZUser getUser(String pid, String code) throws Exception {
        return (HRZZUser) dao.findUnique("from HRZZUser where pid='" + pid + "' and code='" + code + "'");
    }


    @Override
    public HRZZUser getUser(String id, String uid, int state) throws Exception {
        return (HRZZUser) dao.findUnique("from HRZZUser where id='" + id + "' and adduid='" + uid + "' and state='" + state + "'");
    }

    @Override
    public HRZZUser getUser(String id) throws Exception {
        return (HRZZUser) dao.findUnique("from HRZZUser where id='" + id + "'");
    }

    @Override
    public HRZZScore getScore(String code, String pid) throws Exception {
        return (HRZZScore) dao.findUnique("from HRZZScore where code='" + code + "' and pid='" + pid + "'");
    }


}
